package net.projectRPG.src;
import com.jme3.export.Savable;
import com.jme3.export.binary.BinaryImporter;
import com.jme3.export.binary.BinaryExporter;
import java.io.Serializable;
import java.io.IOException;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.InputCapsule;
import com.jme3.export.OutputCapsule;

import com.jme3.bullet.control.CharacterControl;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;

import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.math.Matrix3f;
import com.jme3.math.Ray;
import com.jme3.math.Vector3f;

import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;


public class Creature extends CharacterControl implements Savable, PhysicsCollisionListener  {
    
     public boolean isUnderWater = false;
     private Main main;
     private float distanceToGround = 6.0f;
    
    public Creature (CollisionShape shape, float stepHeight, Main app)
    {
        super(shape, stepHeight);
        main = app;
    }

    // used to calculate if on ground
    public boolean getIsOnGround()
    {
        CollisionResults results = new CollisionResults();
        // 2. Aim the ray from cam loc to cam direction.
        Ray ray = new Ray(main.getCamera().getLocation(), new Vector3f(0f, -1f, 0f));
        // 3. Collect intersections between Ray and Shootables in results list.
        main.getRootNode().collideWith(ray, results);
        // 4. Print the results
        for (int i = 0; i < results.size(); i++) {
          // For each hit, we know distance, impact point, name of geometry.
          float dist = results.getCollision(i).getDistance();
          Vector3f pt = results.getCollision(i).getContactPoint();
          String hit = results.getCollision(i).getGeometry().getName();
          if (results.size() > 0){
            if(dist <= distanceToGround)
            {
                return true;
            }
          }
        } 
        return false;
    }
    
    public void collision(PhysicsCollisionEvent event) {
        if (space == null) {
            super.space = this.getPhysicsSpace();
        }
        
       //  main.player.carriedObject = null;
        
        System.out.println(event.getObjectA());
        System.out.println(event.getObjectB());
        System.out.println("Creature Impact: " + event.getAppliedImpulse());
        
    }    
    
    public void update()
    {
        if(this.getPhysicsLocation().y < main.currentArea.waterLevel )
        {
            isUnderWater = true;
        }else{
            isUnderWater = false;
        }

    }
    
    public void write(JmeExporter ex) throws IOException {
        super.write(ex);
    }

    public void read(JmeImporter im) throws IOException {
        super.read(im);
    }      
}